package com.david4.jizhang.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

/**
 * 
 * @author <a href="mailto:hanxiaojun85@gmail.com">hanxiaojun</a>
 * @since 2010-5-19
 */
public class Util {
	public static boolean isEmpty(String str) {
		return (str == null || str.trim().equals(""));
	}

	public static String convertStreamToString(InputStream is)
			throws UnsupportedEncodingException {
		return convertStreamToString(is, "gbk");
	}

	public static String convertStreamToString(InputStream is, String code)
			throws UnsupportedEncodingException {
		if (isEmpty(code)) {
			code = "gbk";
		}
		BufferedReader reader = new BufferedReader(new InputStreamReader(is,
				code));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
		} catch (IOException e) {
			System.err.print(e.getMessage());
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				System.err.print(e.getMessage());
			}

		}
		return sb.toString();
	}
}
